package com.macbulldev.poc.testgin.domain;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Version;

import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;


@Configurable
@javax.persistence.Entity
@org.springframework.roo.addon.javabean.RooJavaBean
@org.springframework.roo.addon.tostring.RooToString
@org.springframework.roo.addon.entity.RooEntity
public class PolicyType {
	
	private static final Logger LOGGER = Logger.getLogger(PolicyType.class);
	@NotEmpty(message = "Cannot Be Empty")
    private String displayName;
	
	@NotEmpty(message = "Cannot Be Empty")
    private String abbrName;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "policyType")
    private Set<Scenario> scenarios;
    
    private String createUsername;
    private String updateUsername;

    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date createTs;
    
    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date updateTs;
    
    public static PolicyType findPolicyTypeByDisplayNameAndAbbrName(String displayName,String abbrName) {

    	EntityManager em = new PolicyType().entityManager; 
    	
		String hql="select pt from PolicyType pt where upper(trim(pt.displayName))=upper(trim(?)) and upper(trim(pt.abbrName))=upper(trim(?))";

		Query q = em.createQuery(hql);
    	
    	q.setParameter(1, displayName);
    	q.setParameter(2, abbrName);
    	
    	PolicyType retObj = null;
    	
    	try{
    		retObj = (PolicyType)q.getSingleResult();
    	}catch(Exception ex)
    	{
    		LOGGER.debug("query error:  "+ex);
    	}
    	
        return retObj;        

	}
    
    public static PolicyType findPolicyTypeByAbbrName(String abbrName) {

    	EntityManager em = new PolicyType().entityManager; 
    	
		String hql="select pt from PolicyType pt where upper(trim(pt.abbrName))=upper(trim(?))";

		Query q = em.createQuery(hql);
    	
    	q.setParameter(1, abbrName);
    	
    	PolicyType retObj = null;
    	
    	try{
    		retObj = (PolicyType)q.getSingleResult();
    	}catch(Exception ex)
    	{
    		LOGGER.debug("query error:  "+ex);
    	}
    	
        return retObj;        

	}
    
    

	@PersistenceContext    
    transient EntityManager entityManager;

	@Id    
    @GeneratedValue(strategy = GenerationType.AUTO)    
    @Column(name = "id")    
    private Long id;

	@Version    
    @Column(name = "version")    
    private Integer version;

	public Long getId() {    
        return this.id;        
    }

	public void setId(Long id) {    
        this.id = id;        
    }

	public Integer getVersion() {    
        return this.version;        
    }

	public void setVersion(Integer version) {    
        this.version = version;        
    }

	@Transactional    
    public void persist() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.persist(this);        
    }

	@Transactional    
    public void remove() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        if (this.entityManager.contains(this)) {        
            this.entityManager.remove(this);            
        } else {        
            PolicyType attached = this.entityManager.find(PolicyType.class, this.id);            
            this.entityManager.remove(attached);            
        }        
    }

	@Transactional    
    public void flush() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.flush();        
    }

	@Transactional    
    public void merge() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        PolicyType merged = this.entityManager.merge(this);        
        this.entityManager.flush();        
        this.id = merged.getId();        
    }

	public static final EntityManager entityManager() {    
        EntityManager em = new PolicyType().entityManager;        
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");        
        return em;        
    }

	public static long countPolicyTypes() {    
        return (Long) entityManager().createQuery("select count(o) from PolicyType o").getSingleResult();        
    }

	public static List<PolicyType> findAllPolicyTypes() {    
        return entityManager().createQuery("select o from PolicyType o").getResultList();        
    }

	public static PolicyType findPolicyType(Long id) {    
        if (id == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of PolicyType");        
        return entityManager().find(PolicyType.class, id);        
    }

	public static List<PolicyType> findPolicyTypeEntries(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from PolicyType o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }

	public String getDisplayName() {    
        return this.displayName;        
    }

	public void setDisplayName(String displayName) {    
        this.displayName = displayName;        
    }

	public String getAbbrName() {    
        return this.abbrName;        
    }

	public void setAbbrName(String abbrName) {    
        this.abbrName = abbrName;        
    }

	public Set<Scenario> getScenarios() {    
        return this.scenarios;        
    }

	public void setScenarios(Set<Scenario> scenarios) {    
        this.scenarios = scenarios;        
    }

	public String getCreateUsername() {    
        return this.createUsername;        
    }

	public void setCreateUsername(String createUsername) {    
        this.createUsername = createUsername;        
    }

	public String getUpdateUsername() {    
        return this.updateUsername;        
    }

	public void setUpdateUsername(String updateUsername) {    
        this.updateUsername = updateUsername;        
    }

	public Date getCreateTs() {    
        return this.createTs;        
    }

	public void setCreateTs(Date createTs) {    
        this.createTs = createTs;        
    }

	public Date getUpdateTs() {    
        return this.updateTs;        
    }

	public void setUpdateTs(Date updateTs) {    
        this.updateTs = updateTs;        
    }

	public String toString() {    
        StringBuilder sb = new StringBuilder();        
        sb.append("Id: ").append(getId()).append(", ");        
        sb.append("Version: ").append(getVersion()).append(", ");        
        sb.append("DisplayName: ").append(getDisplayName()).append(", ");        
        sb.append("AbbrName: ").append(getAbbrName()).append(", ");        
        sb.append("Scenarios: ").append(getScenarios() == null ? "null" : getScenarios().size()).append(", ");        
        sb.append("CreateUsername: ").append(getCreateUsername()).append(", ");        
        sb.append("UpdateUsername: ").append(getUpdateUsername()).append(", ");        
        sb.append("CreateTs: ").append(getCreateTs()).append(", ");        
        sb.append("UpdateTs: ").append(getUpdateTs());        
        return sb.toString();        
    }
}
